package com.kfm.base.file;

import java.io.File;
import java.util.HashMap;

public class Exer01 {

    public static void main(String[] args) {
        File file = new File("d:/temp");

    }

    public static boolean deleteDir(File dir){
        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            for(File file : files){
                deleteDir(file);
            }
        }
        return dir.delete();
    }

    public static void fileCount(File file){
        if (file == null || !file.exists()){
            throw new RuntimeException();
        }
        HashMap<String, Integer> map = new HashMap<>();

        File[] files = file.listFiles();

        for(File f : files){
            if(f.isFile()){
                String name = f.getName();
                // a.b.c.d.e.f.txt
//                name.lastIndexOf(".");
                String[] split = name.split("\\.");
                String key = split[split.length - 1];
                map.put(key, map.getOrDefault(key, 0) + 1);
            }
        }

    }

    public static long getDirSize(File dir){
        if(dir == null || !dir.exists()){
            throw new RuntimeException();
        }
        long size = 0;
        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            for(File file : files){
                size += getDirSize(file);
            }
        }else{
            size += dir.length();
        }
        return size;
    }


    public static long getDirFileSize(File dir){
        if(dir == null || !dir.exists()){
            throw new RuntimeException();
        }
        long size = 0;

        File[] files = dir.listFiles((f) -> f.isFile());

        for(File file : files){
            size += file.length();
        }
        return size;
    }
}
